package tasknow.ui;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.widgets.Text;

import tasknow.utils.Constants;

public class HelpWindowDisplay extends Dialog {

	// @author A0098740W
	
	protected Object result;
	protected Shell shell;
	private Text textHelpGuide;
	
	private static final String ERROR_UNABLE_TO_READ_HELP_TEXT = "Unabler to read help.txt";
	private static final String HELP_GUIDE_FILE_DIRECTORY = "/resources/help.txt";
	private static final int SELECTION_VALUE = 4300;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public HelpWindowDisplay(Shell parent, int style) {
		super(parent, style);
		setText("SWT Dialog");
		this.open();
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.addMouseTrackListener(new MouseTrackAdapter() {
			@Override
			public void mouseEnter(MouseEvent arg0) {
				shell.setFocus();
			}
		});
		shell.setSize(550, 580);
		shell.setText(getText());
		center(shell);

		textHelpGuide = new Text(shell, SWT.BORDER | SWT.READ_ONLY | SWT.V_SCROLL | SWT.MULTI);
		textHelpGuide.addMouseTrackListener(new MouseTrackAdapter() {
			@Override
			public void mouseEnter(MouseEvent arg0) {
				textHelpGuide.setFocus();
			}
		});
		textHelpGuide.setFont(SWTResourceManager.getFont("Segoe UI", 10, SWT.NORMAL));
		textHelpGuide.setForeground(SWTResourceManager.getColor(SWT.COLOR_CYAN));
		textHelpGuide.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
		textHelpGuide.setBounds(10, 10, 524, 500);
		textHelpGuide.setText(displayHelpGuide());
		textHelpGuide.setSelection(SELECTION_VALUE);

		Button btnExit = new Button(shell, SWT.NONE);
		btnExit.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				shell.dispose();
			}
		});
		btnExit.setBounds(225, 517, 75, 25);
		btnExit.setText("Close");
	}

	private String displayHelpGuide() {
		try {
			InputStream inputStream = getClass().getResourceAsStream(HELP_GUIDE_FILE_DIRECTORY);
			InputStreamReader inputReader = new InputStreamReader(inputStream);
			BufferedReader br = new BufferedReader(inputReader);
			
			String strLine = null;
			String helpGuide = Constants.EMPTY_STRING;
			
			while ((strLine = br.readLine()) != null) {
				helpGuide += (strLine + Constants.NEW_LINE);
			}
			
			br.close();
			
			return helpGuide;
		} catch (IOException e) {
			DisplayAndCommandTextComposite.showToUser(ERROR_UNABLE_TO_READ_HELP_TEXT);
			return Constants.EMPTY_STRING;
		}
	}

	protected void center(Shell shell) {
		Rectangle bds = shell.getDisplay().getBounds();
		Point p = shell.getSize();
		int nLeft = (bds.width - p.x) / 2;
		int nTop = (bds.height - p.y) / 2;

		shell.setBounds(nLeft, nTop, p.x, p.y);
	}

}
